package com.sunwoda.springbootredis.service.impl;

import com.sunwoda.springbootredis.dao.UserDao;
import com.sunwoda.springbootredis.entity.User;
import com.sunwoda.springbootredis.service.OpsListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: springbootredis
 * @description: 操作list
 * @author: SGR
 * @create: 2021-09-13 14:44
 **/
@Service
@Slf4j
public class OpsListServiceImpl implements OpsListService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserDao userDao;

    /**
    * @Description: 操作list
    * @Param:
    * @return:
    * @Author: SGR
    * @Date: 2021-09-13
    */
    @Override
    public void opsList() {

        redisTemplate.setKeySerializer(new StringRedisSerializer());

        List<User> users = userDao.queryAll(new User());

        BoundListOperations list = redisTemplate.boundListOps("list");
        list.leftPush(users);

//        获取list的值0表示第一个元素，-1表示最后一个元素，一次类推
        List range = list.range(1,1);

        log.info("list所有值"+range);

//        获取左边第一个元素
        Object pop = list.leftPop();
        log.info("左边第一个元素"+pop);

        //        获取list的值0表示第一个元素，-1表示最后一个元素，一次类推
        List range2 = list.range(0, -1);

        log.info("list所有值"+range2);

    }
}
